/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeler.setup;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.xml.bind.JAXBException;

import fi.honkalampisaatio.game.xml.KeyValuePair;
import fi.honkalampisaatio.game.xml.LanguageSettings;
import fi.honkalampisaatio.game.xml.SettingsReader;
import fi.honkalampisaatio.game.xml.SettingsWriter;
import fi.honkalampisaatio.lmodeller.LanguageResource;
import fi.honkalampisaatio.lmodeller.xml.GameSettings;

public class LModellerSetup extends JFrame {
	private static final long serialVersionUID = 6978368090968230531L;

	GameSettings gameSettings;
	public final static String SETTINGFILE = "./data/settings.xml";
	BlocksPanel pane;
	PlayerPanel playersPane;
	GameUIPanel gameUIPane;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new LModellerSetup();

	}
	
	public LModellerSetup()
	{
		final JFrame frame = this;
		this.setTitle("Asetukset");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(800, 600));
        this.setLayout(new BorderLayout());
	
		JTabbedPane tabbedPane = new JTabbedPane();
		gameUIPane = new GameUIPanel();
		tabbedPane.addTab("Ulkoasu", gameUIPane);
		playersPane = new PlayerPanel();
		tabbedPane.addTab("Pelaajat", playersPane);
		pane = new BlocksPanel();
		tabbedPane.addTab("Palikat", pane);
		this.add(tabbedPane);
		//The following line enables to use scrolling tabs.
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        
        refreshPanels();

        JPanel pnlLastRow = new JPanel();
        pnlLastRow.setLayout(new GridLayout(1,2,50,50));
        JButton btnCancel = new JButton("Hylkää muutokset");
        btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 20));
        pnlLastRow.add(btnCancel);
        btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				refreshPanels();
				JOptionPane.showMessageDialog(frame,
					    "Asetukset on palautettu.");
			}});
        
        JButton btnSave = new JButton("Tallenna muutokset");
        btnSave.setFont(new Font("Tahoma", Font.PLAIN, 20));
        pnlLastRow.add(btnSave);
        btnSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(saveSettings())
					JOptionPane.showMessageDialog(frame,
							"Asetukset on tallennettu.");
				else
					JOptionPane.showMessageDialog(frame,
							"Tallennus epäonnistui.");
			}});
        
        this.add(pnlLastRow, BorderLayout.PAGE_END);
        this.pack();
        this.setVisible(true);
	}
	
	public boolean refreshPanels()
	{
    	boolean retVal = loadSettings();
    		
    	if(gameSettings==null)
    	{
    		gameSettings = new GameSettings();
    	}
        playersPane.setData(gameSettings.getUsers());
        gameUIPane.setData(gameSettings);
        pane.setData(gameSettings.getBlocks());
        return retVal;
	}
	

    protected boolean loadSettings()
    {
		gameSettings = null;
		try {
			gameSettings = (GameSettings) SettingsReader.getInstance().readSettings(
					SETTINGFILE, GameSettings.class);
		} catch (JAXBException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
    }
    
    protected boolean saveSettings()
    {
		gameSettings = gameUIPane.getData(gameSettings);
		gameSettings.setUsers(playersPane.getUsers());
		gameSettings.setBlocks(pane.getBlocks());

		try {
			SettingsWriter.getInstance().writeGameSettings(SETTINGFILE, gameSettings);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
    }

}
